package com.tanxi521.tnweb.service.impl;

import com.tanxi521.tnweb.entity.Player;
import com.tanxi521.tnweb.mapper.PlayerMapper;
import com.tanxi521.tnweb.service.PlayerService;
import com.tanxi521.tnweb.utils.PlayerException;
import com.tanxi521.tnweb.utils.PlayerExceptionEnum;
import com.tanxi521.tnweb.utils.SHA256Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class PlayerServiceImpl implements PlayerService {

    @Autowired
    PlayerMapper playerMapper;

    @Override
    public Player login(String playerName, String pass) {
        Player player = new Player();
        player.setRealname(playerName);
        List<Player> players = playerMapper.select(player);
        Player player1 = players.get(0);
        if (player1 == null) {
            throw new PlayerException(PlayerExceptionEnum.UNKNOWN_ACCOUNT_EXCEPTION);
        }
        String password = player1.getPassword();
        String inPass = "$SHA$" + "299539f223921b7c$" + SHA256Util.getSHA256(pass + "299539f223921b7c");
        if (password.equals(inPass)) {
            return player1;
        } else {
            throw new PlayerException(PlayerExceptionEnum.INCORRECT_PASSWORD_EXCEPTION);
        }
    }
}
